//
//  CartoonViewController.m
//  BuffetNews
//
//  Created by weidawei on 14/11/9.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CartoonViewController.h"
#import "CartoonCell.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "UIImage+Tint.h"
#import "UIColor+myColor.h"
#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

@interface CartoonViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView * collectionView;
@property (nonatomic, retain) NSMutableArray * articleArr;
@property (nonatomic, retain) NSMutableArray * photoArr;
@property (nonatomic, retain) NSString * block_color;
@property (nonatomic, retain) NSString * bgimage;
@property (nonatomic, retain) NSMutableArray * arrTitle;
@property (nonatomic, retain) MBProgressHUD *loading;
@end

@implementation CartoonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.articleArr = [NSMutableArray array];
        self.photoArr = [NSMutableArray array];
        self.arrTitle = [NSMutableArray array];
    }
    return self;
}

- (void)startData
{
    NSLog(@"apistring = %@", self.apiString);
    
    BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
    
    if (!reachable) {
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
        
        
        
    } else {
    
    
    [NetHandler networkWithURL:self.apiString comaletion:^(id result) {
        self.articleArr = [[result objectForKey:@"data"] objectForKey:@"articles"];
        for (NSDictionary *dic in self.articleArr) {
            NSString *string = [[[dic objectForKey:@"media"] lastObject] objectForKey:@"m_url"];
            [self.arrTitle addObject:[dic objectForKey:@"title"]];
            [self.photoArr addObject:string];
        }
        
       
        self.block_color = [[[result objectForKey:@"data"] objectForKey:@"block_info"] objectForKey:@"block_color"];
        NSDictionary * dicData = [result objectForKey:@"data"];
        NSDictionary * dic1 = [dicData objectForKey:@"ipadconfig"];
        NSMutableArray * arrpages = [NSMutableArray array];
        arrpages = [dic1 objectForKey:@"pages"];
        NSDictionary * dic2 = [arrpages[0] objectForKey:@"diy"];
        self.bgimage = [dic2 objectForKey:@"bgimage_url"];
        
        [self loadingView];
        [self.collectionView reloadData];
        [self.loading removeFromSuperview];
        

    }];
    }
}

-(BOOL)isReachableViaWWAN
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

- (void)loadingView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flow.minimumInteritemSpacing = 2;
    //cell行之间的间距
    flow.minimumLineSpacing = 20;
    flow.sectionInset = UIEdgeInsetsMake(62, 0, 1, 1);
    UIImageView * bgimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    [bgimage sd_setImageWithURL:[NSURL URLWithString:self.bgimage]];
    bgimage.backgroundColor = [UIColor colorWithHexString:self.block_color];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5, self.view.frame.size.height / 50 * 46, 45, 45);
    [button addTarget:self action:@selector(blocl:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + 10, self.view.frame.origin.y, self.view.frame.size.width - 20, self.view.frame.size.height - 50) collectionViewLayout:flow];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:bgimage];
    [self.view addSubview:button];
    NSLog(@"bagim = %@", self.bgimage);
    [self.collectionView registerClass:[CartoonCell class] forCellWithReuseIdentifier:@"pool"];
    
    [_collectionView release];
    [self loading];
}

- (void)loadingjuhua
{
    self.loading = [[[MBProgressHUD alloc] initWithFrame:self.view.frame]autorelease];
    self.loading.labelText = @"加载....";
    [self.loading setMode:MBProgressHUDModeAnnularDeterminate];
    [self.loading show:YES];
    [self.view addSubview:self.loading];
}

- (void)blocl:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.photoArr count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.photoArr[indexPath.item]]];
    cell.title.text = self.arrTitle[indexPath.item];
    
    
    return cell;
}

- (void)dealloc
{
    [_collectionView release];
    [_photoArr release];
    [_block_color release];
    [_bgimage release];
    [_arrTitle release];
    [_loading release];
    [_apiString release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loading];
    // Do any additional setup after loading the view.
    [self startData];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
